package cn.com.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 微网联通 乐信 web.lmobile.cn 调用
 * */
public class SendMsgUtil {
	
	
	/****** 短信配置信息 *******/

	// 账号
	public static final String sname = "dlsmkjbj";

	// 密码
	public static final String spwd = "bjbj1234";

	/************** 产品类别 *****************/

	// 行业通知短信
	public static final String sprdid_notice = "1012888";

	// 会员营销短信
	public static final String sprdid_associator = "1012888";

	// 语音行业短信
	public static final String sprdid_voice = "1012888";

	// 行业触发彩信
	public static final String sprdid_multimedia = "1012888";

	/****************************************/

	// 企业代码
	public static final String scorpid = "";

	// 签名
	public static final String sign = "【首媒中国】";

	/**
	 * 
	 * @Description: 发短信
	 * @param smsinfo
	 *            短信内容
	 * @param phones
	 *            手机号，多个手机号,拼接
	 * @param sprdid
	 *            产品类别编码
	 * @param @return
	 */
	public static String sendSMS(String smsinfo, String phones, String sprdid) {
		try {
			// 拼接请求参数
			String postData = "sname=" + sname + "&spwd=" + spwd + "&scorpid=" + scorpid + "&sprdid=" + sprdid
					+ "&sdst=" + phones + "&smsg=" + smsinfo + sign;
			// 发送POST请求
			URL url = new URL("http://cf.51welink.com/submitdata/Service.asmx/g_Submit");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setUseCaches(false);
			conn.setDoOutput(true);

			conn.setRequestProperty("Content-Length", "" + postData.length());
			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
			out.write(postData);
			out.flush();
			out.close();

			// 获取响应状态
			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				System.out.println("connect failed!");
				return "";
			}
			// 获取响应内容体
			String line, result = "";
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			while ((line = in.readLine()) != null) {
				result += line + "\n";
			}
			in.close();
			if(result.contains("提交成功")){
				return result;
			}else{
				return "";
			}
			
		} catch (IOException e) {
			e.printStackTrace(System.out);
		}
		return "";
	}
}
